const md5 = require('md5.js');
const app = getApp();
// var urls = {
//   api: 'https://api.xizengkeji.com/',
//   wechat: 'https://wechat.xizengkeji.com/',
//   live: 'https://live.xizengkeji.com/',
//   store: 'https://store.xizengkeji.com/'
// }
var urls = {
  api: 'https://api.rootphp.com/',
  wechat: 'https://wechat.rootphp.com/',
  store: 'https://store.rootphp.com/',
  live: 'https://live.rootphp.com/'
}
//请求封装
function getHeader(data, url,type) {
  const timestamp = Date.parse(new Date()) / 1000
  const obj = {
    timestamp: timestamp,
    domain: type?type:getApp().globalData.baseUrl,
    key: 'XIZENGB7D0C9D279892857DE5490AB856772FE',
    pathinfo: url
  }
  if (data) {
    var sign_obj = objKeySort({
      ...obj,
      ...data,
    })
    console.log('1')
  } else {
    var sign_obj = objKeySort({
      ...obj,
    })
    console.log('2')
  }
  const sign_Arr = [];
  for (let i in sign_obj) {
    sign_Arr.push((i + '=' + sign_obj[i]))
  }
  var string = sign_Arr.join("&")
  console.log('sign-----', string)
  console.log('Md5------', md5.md5(string))
  const sign_Last = md5.md5(string).toLowerCase()
  if (wx.getStorageSync('token')) {
    var header = {
      'sign': sign_Last,
      'token': wx.getStorageSync('token'),
      'timestamp': timestamp,
      // 'debug': 'true'
    }
  } else {
    var header = {
      'sign': sign_Last,
      'token': '',
      'timestamp': timestamp,
      // 'debug': 'true'
    }
  }
  if (url == 'v1/bindMobile') {
    console.log('ggg')
  }
  // header.Cookie= 'PHPSESSID=' +wx.getStorageSync('session') + '; path=/; HttpOnly'
  header.Cookie = ' PHPSESSID=42d26dafc5c8f2c40963b0e6be5b7626; path=/'
  return header
  console.log(header)
}

function objKeySort(obj) {
  console.log(obj)
  var newkey = Object.keys(obj).sort();
  var newObj = {};
  for (var i = 0; i < newkey.length; i++) {
    newObj[newkey[i]] = obj[newkey[i]];
  }
  return newObj;
}
//请求封装
function request(url, data, method, onSuccess, onFailed, type) {
  wx.request({
    url: !type ? getApp().globalData.baseUrl + url : urls[type] + url,
    data: data,
    method: method,
    header: getHeader(data, url, type ? urls[type] : getApp().globalData.baseUrl),
    success: function (res) {
      if (res.data) {
        /** start 根据需求 接口的返回状态码进行处理 */
        if (res.statusCode == 200 || res.statusCode == 201) {
          wx.setStorageSync('session', res.header['Set-Cookie'])
          onSuccess(res.data); //request success
        } else {
          onFailed(res.data); //request failed
        }
        /** end 处理结束*/
      }
    },
    fail: function (error) {
      console.log('接口请求失败，该方法执行清除本地所有缓存')
    }
  })
}
//get请求
function get(url, data, onSuccess, onFailed, type) {
  request(url, data, "GET", onSuccess, onFailed, type);
}
//post请求
function post(url, data, onSuccess, onFailed, type) {
  request(url, data, "POST", onSuccess, onFailed, type);
}
//put请求
function put(url, data, onSuccess, onFailed, type) {
  request(url, data, "PUT", onSuccess, onFailed, type);
}
//delete请求
function Delete(url, data, onSuccess, onFailed, type) {
  request(url, data, "DELETE", onSuccess, onFailed, type);
}
//模板导出
module.exports = {
  getRequest: get,
  postRequest: post,
  putRequest: put,
  deleteRequest: Delete,
  getHeader: getHeader
}